import Mock from "mockjs";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
let Products: any[] = [];
const productCount = 100;

function param2Obj(url: string) {
  const search = url.split("?")[1];
  if (!search) {
    return {};
  }
  return JSON.parse(
    '{"' +
      decodeURIComponent(search)
        .replace(/"/g, '\\"')
        .replace(/&/g, '","')
        .replace(/=/g, '":"') +
      '"}'
  );
}

for (let i = 0; i < productCount; i++) {
  Products.push(
    Mock.mock({
      id: Mock.Random.guid(),
      name: Mock.Random.ctitle(3, 10), // 商品名称，长度在3到10个字符之间
      description: Mock.Random.cparagraph(), // 商品描述
      price: Mock.Random.float(10, 1000, 2, 2), // 商品价格，范围从10到1000，保留两位小数
      category: Mock.Random.pick([
        "电子产品",
        "家居用品",
        "图书",
        "食品",
        "服饰",
      ]), // 商品类别
      "stock|1-100": 50, // 库存数量，范围从1到100
      createdAt: Mock.Random.date(), // 创建日期
      image: Mock.Random.dataImage("200x200", Mock.Random.cword(3, 6)), // 随机生成的商品图片
    })
  );
}

export const productApi = {
  /**
   * 获取商品列表
   * @param name, page, limit
   */
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  getProductList: (config: { url: any }) => {
    const { name, page = 1, limit = 10 } = param2Obj(config.url);
    const mockList = Products.filter((product) => {
      if (name && product.name.indexOf(name) === -1) return false;
      return true;
    });
    const pageList = mockList.filter(
      (item, index) => index < limit * page && index >= limit * (page - 1)
    );
    return {
      code: 20000,
      count: mockList.length,
      list: pageList,
    };
  },
  /**
   * 添加商品
   * @param name, description, price, category, stock, image
   */
  createProduct: (config: { body: string }) => {
    const { name, description, price, category, stock, image } = JSON.parse(
      config.body
    );
    Products.unshift({
      id: Mock.Random.guid(),
      name,
      description,
      price,
      category:
        category ||
        Mock.Random.pick(["电子产品", "家居用品", "图书", "食品", "服饰"]), // 如果没有提供类别，则随机选择一个
      stock,
      image: image || Mock.Random.dataImage("200x100", "商品图片"), // 如果没有提供图片，则生成一个
      createdAt: Mock.Random.date(),
    });
    return {
      code: 20000,
      data: {
        message: "添加成功",
      },
    };
  },
  /**
   * 删除商品
   * @param id
   */
  deleteProduct: (config: { body: string }) => {
    const { id } = JSON.parse(config.body);
    if (!id) {
      return { code: -999, message: "参数不正确" };
    } else {
      Products = Products.filter((p) => p.id !== id);
      return { code: 20000, message: "删除成功" };
    }
  },
  /**
   * 更新商品信息
   * @param id, name, description, price, category, stock, image
   */
  updateProduct: (config: { body: string }) => {
    const { id, name, description, price, category, stock, image } = JSON.parse(
      config.body
    );
    const productIndex = Products.findIndex((p) => p.id === id);
    if (productIndex !== -1) {
      Products[productIndex] = {
        ...Products[productIndex],
        name,
        description,
        price,
        category:
          category ||
          Mock.Random.pick(["电子产品", "家居用品", "图书", "食品", "服饰"]), // 如果没有提供类别，则随机选择一个
        stock,
        image,
      };
    }
    return { code: 20000, data: { message: "编辑成功" } };
  },
};
